// src/utils/auth.js

export function getToken() {
  return localStorage.getItem('token') || ''
}

export function setToken(token) {
  localStorage.setItem('token', token)
}

export function removeToken() {
  localStorage.removeItem('token')
}

export function isTokenExpired(token) {
  if (!token) return true

  try {
    // 如果是JWT token，解析exp字段
    const payload = JSON.parse(atob(token.split('.')[1]))
    const expired = payload.exp < Date.now() / 1000
    if (expired) console.warn('Token已过期')
    return expired
  } catch (e) {
    console.warn('Token解析失败:', e)
    return true // 解析失败视为过期
  }
}
